package com.example.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;

public class kafkaStrUtil {
    //对象字符串转Integer
    public static Integer transInt(String objectString) {
        if (objectString == null) {
            return null;
        }
        if (StrUtil.equals("null", objectString)) {
            return null;
        }
        return Integer.valueOf(objectString);
    }


    public static Date transDate(String objectString){
       if (objectString == null) {
           return null;
       }
       if (StrUtil.equals("null", objectString)) {
           return null;
       }
       return  DateUtil.parse(objectString, DatePattern.NORM_DATETIME_PATTERN);
    }

    public static Long transLong(String objectString){
        if (objectString == null) {
            return null;
        }
        if (StrUtil.equals("null", objectString)) {
            return null;
        }
        return  Long.valueOf(objectString);
    }

    public static BigDecimal transBigDecimal(String objectString){
        if (objectString == null) {
            return null;
        }
        if (StrUtil.equals("null", objectString)) {
            return null;
        }
        return  NumberUtil.toBigDecimal(objectString);
    }


    /**
     * json转KafkaMsgDto
     * @param jsonObject
     * @param prefix
     * @param beanClass
     * @return
     */
    public static <T> T  jsonToKafkaDto(JSONObject jsonObject, String prefix, Class<T> beanClass) {
        T bean;

        try {
            bean = beanClass.newInstance();

            for (Field field : beanClass.getDeclaredFields()) {
                String fieldName = StrUtil.toUnderlineCase(field.getName());
                String jsonKey = "";
                if (StrUtil.isNotEmpty(prefix)) {
                    jsonKey = prefix + "_" + fieldName.toUpperCase();
                } else {
                    jsonKey = fieldName;

                }
                if (jsonObject.containsKey(jsonKey)) {
                    field.setAccessible(true);
                    field.set(bean, getValue(jsonObject.getString(jsonKey), field.getType()));
                }
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Json结构解析异常" + jsonObject.toJSONString() + "   ;error:", e);
        }

        return bean;
    }

    private static Object getValue(String value, Class<?> type){
        if (value != null){
            if (type.isAssignableFrom(Integer.class) || type.isAssignableFrom(int.class))
                return transInt(value);
            else if (type.isAssignableFrom(Double.class) || type.isAssignableFrom(double.class))
                return Double.valueOf(value);
            else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class))
                return Boolean.valueOf(value);
            else if (type.isAssignableFrom(Date.class))
                return transDate(value);
            else if (type.isAssignableFrom(BigDecimal.class))
                return BigDecimal.valueOf(Long.parseLong(value));
            else if (type.isAssignableFrom(Long.class) || type.isAssignableFrom(long.class))
                return Long.valueOf(value);
            else if (type.isAssignableFrom(String.class))
                return value;
            else
                return value;
        }
        return null;
    }


    /**
     * 16进制字符串转byte数组
     *
     * @param hexString
     * @return
     */
    public static byte[] getByteArray(String hexString) {
        byte[] result = new byte[hexString.length() >> 1];
        for (int i = 0; i < result.length; i++) {
            char a = hexString.charAt(2 * i);
            char b = hexString.charAt(2 * i + 1);
            String ab = new String(new char[]{a, b});
            result[i] = (byte) Integer.valueOf(ab, 16).intValue();
        }

        return result;
    }

}
